﻿/*-----------------------------------------------
// File: ViewRespectCtrl.cs
// Description: 渲染层战斗期望管理器 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Battle.View
{
    public class ViewRespectCtrl 
    {
        private static ViewRespectCtrl instance = null;
        public static ViewRespectCtrl GetInstance() {
            if (instance == null)
                instance = new ViewRespectCtrl();
            return instance;
        }
        private ViewRespectCtrl() { }

        List<RespectBase> respectCache = new List<RespectBase>();
        List<ResponseBase> responseCache = new List<ResponseBase>();

        #region GetRespect
        public int GetRespectCount() { return respectCache.Count; }
        public AttackRespect GetRespect(ProtoBuf.Message.SkillOnceCast onceCast) {
            foreach (var _respect in respectCache) {
                var respect = _respect as AttackRespect;
                if (respect != null && respect.sourceUid == onceCast.sourceUid && respect.skillType == (SkillType)onceCast.skillType && respect.releaseId == onceCast.releaseId)
                {
                    return respect;
                }
            }
            return null;
        }
        #endregion
        #region GetResponse
        public int GetResponseCount() { return responseCache.Count; }
        public ResponseBase GetResponse(RespectBase _respect) {
            if (_respect.GetType() == typeof(AttackRespect))
            {
                var respect = _respect as AttackRespect;
                foreach (var _response in responseCache)
                {
                    var response = _response as AttackResponse;
                    if (response != null && response.sourceUid == respect.sourceUid && response.skillType == respect.skillType && response.releaseId == respect.releaseId)
                    {
                        return response;
                    }
                }
            }
            else if (_respect.GetType() == typeof(ProjectileRespect)) {
                var respect = _respect as ProjectileRespect;
                foreach (var _response in responseCache) {
                    var response = _response as ProjectileResponse;
                    if (response != null && response.sourceUid == respect.sourceUid && response.skillType == respect.skillType && response.releaseId == respect.releaseId) {
                        return response;
                    }
                }
            }

            return null;
        }
        #endregion

        #region AddRespect
        public void AddRespect(RespectBase respect, OnResponse onResponse) {
            respect.AddResponseCallBack(onResponse);
            respectCache.Add(respect);
        }
        /// <summary>
        /// 添加AttackRespect
        /// PS：攻击动画到达Release点时添加
        /// </summary>
        /// <param name="respect"></param>
        public void AddRespect(string uid, SkillType skillType, string releaseId, OnResponse onResponse)
        {
            var respect = new AttackRespect(uid, skillType, releaseId);
            respect.AddResponseCallBack(onResponse);
            respectCache.Add(respect);
        }
        #endregion
        #region AddResponse
        public void AddResponse(ProtoBuf.Message.SkillOnceCast onceCast) {
            if (onceCast.roundNum != roundNum) {
                Debug.LogError("Response Error. Round Num Not Match!! Current:" + roundNum + " Remote:" + onceCast.roundNum);
                return;
            }
            
            responseCache.Add(new AttackResponse(onceCast));
        }
        public void AddResponse(ProtoBuf.Message.SCShoot msg) {
            if (msg.roundNum != roundNum)
            {
                Debug.LogError("Response Error. Round Num Not Match!! Current:" + roundNum + " Remote:" + msg.roundNum);
            }
            responseCache.Add(new ProjectileResponse(msg));
        }
        public void AddResponse(ProtoBuf.Message.SCShootProj msg) {
            if (msg.roundNum != roundNum) {
                Debug.LogError("Response Error. Round Num Not Match!! Current:" + roundNum + " Remote:" + msg.roundNum);
            }
            responseCache.Add(new ProjectileResponse(msg));
        }
        #endregion
        public void Tick() {
            List<ResponseBase> needRemoveResponses = new List<ResponseBase>();
            List<RespectBase> needRemoveRespects = new List<RespectBase>();
            foreach (var respect in respectCache) {
                var response = GetResponse(respect);
                if (response != null) {
                    respect.ReceiveResponse(response);
                    needRemoveRespects.Add(respect);
                    needRemoveResponses.Add(response);
                }
            }
            foreach (var respect in needRemoveRespects) {
                respectCache.Remove(respect);
            }
            foreach (var response in needRemoveResponses) {
                responseCache.Remove(response);
            }
        }
        public void ClearRespect() {
            respectCache.Clear();
        }
        public void ClearResponse() {
            responseCache.Clear();
        }

        public void NewRound() {
            if (respectCache.Count > 0 || responseCache.Count > 0) {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.AppendLine("Have Redundancy Respect or Response: ");
                foreach (var respect in respectCache) {
                    sb.AppendLine(respect.GetDesc());
                }
                foreach (var response in responseCache) {
                    sb.AppendLine(response.GetDesc());
                }
                Debug.LogWarning(sb.ToString());
            }
            ClearRespect();
            ClearResponse();
        }

        public int roundNum {
            get { return BattleViewCtrl.GetInstance().GetRoundNum(); }
        }

    }
}